//引入模块
import express from "express";
import expressSession from "express-session";
import bodyParser from "body-parser";
import cookieParser from "cookie-parser";
import uuid from "uuid";
import multer from "multer";
import DB from "./libs/DBHelper"
import url from "url";
// import cors from "cors";

// const fs = require("fs")
// const path = require("path")

//创建服务
const server = express();

//静态资源服务器
server.use(express.static("./../src/"))



//预设  白名单
let WhiteList = ["/api/login", "/api/reg", "/api/checkuser"]
//拦截器
server.all("*", function (request, response, next) {
    response.set({
        "Content-Type": "application/json;charset=utf-8",
        "Access-Control-Allow-Origin": "*"//cors
    })

    //截取路径
    // console.log(request.session["isLogin"])
    // // url.parse(request.originalUrl, true).pathname
    // if (WhiteList.indexOf(url.parse(request.originalUrl, true).pathname) != -1) {
    //     next()
    // } else {
    //     if (request.session["isLogin"] != undefined || request.session["isLogin"] != null) {
    //         next()
    //     } else {
    //         response.json({
    //             msg: "您还未登录,请登录!",
    //             status: -100
    //         })
    //     }
    // }
    next()
})



//配置中间件
server.use(expressSession({
    name: "meanseon.cn",
    secret: "meanseon.cn",
    saveUninitialized: true,//不管有没有都会初始化一个COOKIE在我浏览器里
    genid: function () {
        return uuid.v1
    },
    resave: true,
    maxAge: 1000 * 60 * 60 * 24,

}))
server.use(bodyParser.urlencoded({
    extended: false
}))
server.use(bodyParser.json())
server.use(cookieParser("meanseon.cn"))







//保安大爷管理api
server.use("/api", require("./routes/ApiRoute").apiRouter)


// server.use(cors({
//     origin: ['http://127.0.0.1:5500/src/login.html', "http://127.0.0.1:5500/src/reg.html"],
//     methods: ['GET', 'POST'],
//     alloweHeaders: ['Conten-Type', 'Authorization']
// }))




//设置服务器监听
server.listen(8080, function () {
    console.log("服务器启动完毕")
})